package com.ruoyi.algorithm.dict.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.algorithm.dict.entity.Dictionary;
import com.ruoyi.algorithm.dict.entity.DictionaryType;
import com.ruoyi.algorithm.dict.service.DictionaryService;
import com.beihu.sightplus.common.response.PageResult;
import com.beihu.sightplus.common.response.PageUtils;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：yanfa-03
 * @date ：Created in 2021/7/15/11:15
 * @description ：字典控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("dict")
@Api(tags = "字典")
@Slf4j
public class DictController {
    @Autowired
    DictionaryService dictionaryService;

    @GetMapping("pageDict")
    @ApiOperation("获取字典分页")
    public ResultData<PageResult> pageDict(@ApiParam("当前页码") Integer current,
                                           @ApiParam("每页大小") Integer size,
                                           @ApiParam("字典实体筛选条件") Dictionary dictionary,
                                           @ApiParam("排序依据") String orderBy,
                                           @ApiParam("正序倒序（asc|desc）") String order) {
        boolean isAsc = false;
        if ("asc".equals(order)) {
            isAsc = true;
        }
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 10;
        }
        Page<Dictionary> dictionaryPage = new Page<>(current, size);
        Page<Dictionary> page = null;
        try {
            page = dictionaryService.page(dictionaryPage, dictionary, orderBy, isAsc);
        } catch (NoSuchFieldException e) {
            log.warn(e.toString());
            return new ResultData<>(400, "没有该字段，无法排序", null);
        }
        return ResultData.success(PageUtils.getPageResult(page));
    }

    @GetMapping("pageDictType")
    @ApiOperation("获取字典类型分页")

    public ResultData<PageResult> pageDictType(@ApiParam("当前页码") Integer current,
                                               @ApiParam("每页大小") Integer size,
                                               @ApiParam("字典类型实体筛选条件") DictionaryType dictionaryType,
                                               @ApiParam("排序字段") String orderBy,
                                               @ApiParam("正序倒序，asc|desc") String order) {
        boolean isAsc = false;
        if ("asc".equals(order)) {
            isAsc = true;
        }
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 10;
        }
        Page<DictionaryType> dictionaryPage = new Page<>(current, size);
        Page<DictionaryType> page = null;
        try {
            page = dictionaryService.pageDictType(dictionaryPage, dictionaryType, orderBy, isAsc);
        } catch (NoSuchFieldException e) {
            log.warn(e.toString());
            return new ResultData<>(400, "没有该字段，无法排序", null);
        }
        return ResultData.success(PageUtils.getPageResult(page));
    }

    @GetMapping("getValueByKey")
    @ApiOperation("根据typeKey&key获取value")
    public ResultData<String> getValueByKey(@RequestParam String typeKey, String key) {
        String valueByKey = dictionaryService.getValueByKey(typeKey, key);
        return ResultData.success(valueByKey);
    }

    @GetMapping("getDictListByTypeId")
    @ApiOperation("根据字典类型key获取字典分页")
    public ResultData<?> getDictListByTypeId(@ApiParam("字典类型Id")@RequestParam String typeKey,
                                             @ApiParam("当前页码") Integer current,
                                             @ApiParam("每页大小") Integer size) {
        if (current == null) {
            current = 1;
        }
        if (size == null) {
            size = 10;
        }
        IPage<Dictionary> page = dictionaryService.getDictListByTypeId(new Page<>(current,size), typeKey);
        PageResult pageResult = PageUtils.getPageResult(page);
        return ResultData.success(pageResult);
    }

    @PutMapping("addDictionary")
    @ApiOperation("新增字典")
    public ResultData<?> addDictionary(@RequestBody Dictionary dictionary) {
        dictionaryService.add(dictionary);
        return ResultData.success();
    }

    @PostMapping("updateDictionary")
    @ApiOperation("编辑字典")
    public ResultData<?> updateDictionary(@RequestBody Dictionary dictionary) {
        boolean update = dictionaryService.update(dictionary);
        if (update) {
            return ResultData.success();
        }
        return ResultData.success("更新了0行");
    }

    @DeleteMapping("deleteDictionary")
    @ApiOperation("删除字典")
    public ResultData<?> deleteDictionary(Long id) {
        dictionaryService.deleteById(id);
        return ResultData.success();
    }

    @PutMapping("addDictionaryType")
    @ApiOperation("新增字典类型")
    public ResultData<?> addDictionaryType(@RequestBody DictionaryType dictionaryType) {
        dictionaryService.addDictType(dictionaryType);
        return ResultData.success();
    }

    @PostMapping("updateDictionaryType")
    @ApiOperation("编辑字典类型")
    public ResultData<?> updateDictionaryType(@RequestBody DictionaryType dictionaryType) {
        boolean updated = dictionaryService.updateDictType(dictionaryType);
        if (updated) {
            return ResultData.success();
        }
        return ResultData.success("更新了0行");
    }

    @DeleteMapping("deleteDictionaryType")
    @ApiOperation("删除字典类型")
    public ResultData<?> deleteDictionaryType(Long id) {
        dictionaryService.deleteDictTypeById(id);
        return ResultData.success();
    }
}
